# -*- coding: utf-8 -*-
#
# Copyright (C) 2007, İlker Kesen <ilker.kde@gmail.com>
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# Please read the COPYING file.

import variables as scroppy
import piksemel
import urllib2

class TopTags:
    def __init__(self):
        self.data = piksemel.parseString(urllib2.urlopen("%s/%s/tag/toptags.xml" % (scroppy.url, scroppy.scrobblerVersion)).read())

    def topTags(self):
        (toptags, tags, variable) = ([], self.data.tags(), 1)
        while variable != None:
            try:
                variable = tags.next()
            except StopIteration:
                break
            else:
                name   = variable.getAttribute("name")
                count  = variable.getAttribute("count")
                url    = variable.getAttribute("url")
                toptags.append((name, count, url))
        return toptags

class TopArtists:
    def __init__(self, tag):
        tag = tag.replace(" ", "%20")
        self.data = piksemel.parseString(urllib2.urlopen("%s/%s/tag/%s/topartists.xml" % (scroppy.url, scroppy.scrobblerVersion, tag)).read())

    def name(self):
        return self.data.getAttribute("name")

    def count(self):
        return self.data.getAttribute("count")

    def topArtists(self):
        (topartists, tags, variable) = ([], self.data.tags(), 1)
        while variable != None:
            try:
                variable = tags.next()
            except StopIteration:
                break
            else:
                name   = variable.getAttribute("name")
                count  = variable.getAttribute("count")
                url    = variable.getTagData("url")
                thumb  = variable.getTagData("thumbnail")
                image  = variable.getTagData("image")
                topartists.append((name, count, url, thumb, image))
        return topartists

class TopAlbums:
    def __init__(self, tag):
        tag = tag.replace(" ", "%20")
        self.data = piksemel.parseString(urllib2.urlopen("%s/%s/tag/%s/topalbums.xml" % (scroppy.url, scroppy.scrobblerVersion, tag)).read())

    def name(self):
        return self.data.getAttribute("name")

    def count(self):
        return self.data.getAttribute("count")

    def topAlbums(self):
        (topalbums, tags, variable) = ([], self.data.tags(), 1)
        while variable != None:
            try:
                variable = tags.next()
            except StopIteration:
                break
            else:
                name   = variable.getAttribute("name")
                count  = variable.getAttribute("count")
                artist = variable.getTag("artist").getAttribute("name")
                url    = variable.getTagData("url")
                small  = variable.getTag("coverart").getTagData("small")
                medium = variable.getTag("coverart").getTagData("medium")
                large  = variable.getTag("coverart").getTagData("large")
                topalbums.append((name, count, artist, url, small, medium, large))
        return topalbums

class TopTracks:
    def __init__(self, tag):
        tag = tag.replace(" ", "%20")
        self.data = piksemel.parseString(urllib2.urlopen("%s/%s/tag/%s/toptracks.xml" % (scroppy.url, scroppy.scrobblerVersion, tag)).read())

    def name(self):
        return self.data.getAttribute("name")

    def count(self):
        return self.data.getAttribute("count")

    def topTracks(self):
        (toptracks, tags, variable) = ([], self.data.tags(), 1)
        while variable != None:
            try:
                variable = tags.next()
            except StopIteration:
                break
            else:
                name   = variable.getAttribute("name")
                count  = variable.getAttribute("count")
                artist = variable.getTag("artist").getAttribute("name")
                url    = variable.getTagData("url")
                thumb = variable.getTagData("thumbnail")
                image  = variable.getTagData("image")
                toptracks.append((name, count, artist, url, thumb, image))
        return toptracks

